import React, { Component } from 'react'
import { ListBox } from '../../../components'
import { fetchData } from '../../../components/AjaxConsumer'
import ContractNumberForm from './ContractNumberForm'
import { Button, Modal, Form, Input, Radio, Upload, Icon } from 'antd';
export default class RawMaterialConversion extends Component {
    constructor(props) {
        super(props);
        this.state = {
            tableColumn: {},
            searchComponentData: [],
            visible: false,
            formData:{},
            submitUrl:'',
        };
        //行操作列表
        this.rowOperation = [
        ];
        //header 操作列表
        this.headerOperation = [
            {
                handleName: 'edit',
                showName: '合同号修改',
                iconClass: 'edit',
                permission: 'rawunit-edit'
            }
        ];

        //table列表展示列配置
        //showName、columnName 必有属性， columnWidth 可选，默认100, columnHidden: true 列隐藏
        this.tableColumn = {
            storageKey: 'listOdsRawUnitConfig',
            data: [
                {
                    showName: 'storageSnId',
                    columnName: 'storageSnId',
                    columnHidden: true,
                    foreverHidden: true
                }, {
                    showName: '合同编号',
                    columnName: 'contractNo',
                    columnWidth: 130
                }, {
                    showName: '条码',
                    columnName: 'barcode',
                    columnWidth: 130
                }, {
                    showName: '型号',
                    columnName: 'model'
                }, {
                    showName: '型号类型',
                    columnName: 'modelType'
                }, {
                    showName: '型号描述',
                    columnName: 'modelDesc'
                }, {
                    showName: '长度',
                    columnName: 'amount'
                }, {
                    showName: '单位',
                    columnName: 'unit'
                },
                //{
                //    showName: '型号类型',
                //    columnName: 'modelType',
                //    showOption: [//页面显示配置
                //        {
                //            optionKey: '0',
                //            optionShow: '正品'
                //        }, {
                //            optionKey: '1',
                //            optionShow: '样品'
                //        }
                //    ]
                //}, {
                //    showName: '采购订单号',
                //    columnName: 'purchaseNo'
                //}, {
                //    showName: '仓库',
                //    columnName: 'whCode'
                //}, {
                //    showName: '区域',
                //    columnName: 'wdCode'
                //}, {
                //    showName: '库区',
                //    columnName: 'waCode'
                //},
                {
                    showName: ' 库位',
                    columnName: 'wlCode'
                }, {
                    showName: '状态',
                    columnName: 'barcodeStatus',
                    showOption: [//页面显示配置
                        {
                            optionKey: '0',
                            optionShow: '可用'
                        }, {
                            optionKey: '1',
                            optionShow: '冻结'
                        }, {
                            optionKey: '2',
                            optionShow: '已出库'
                        }, {
                            optionKey: '1',
                            optionShow: '报废'
                        }
                    ]
                }, {
                    showName: '冻结原因',
                    columnName: 'lockReason',
                    showOption: [//页面显示配置
                        {
                            optionKey: '0',
                            optionShow: '下线即冻结'
                        }, {
                            optionKey: '1',
                            optionShow: '抽检冻结'
                        }, {
                            optionKey: '2',
                            optionShow: '质量冻结'
                        }, {
                            optionKey: '4',
                            optionShow: '手工冻结'
                        }
                    ]
                }
            ]
        };

        //搜索属性配置
        this.searchComponentData = [
            {
                showName: '型号类型',
                name: 'modelType',
                type: 'select',
                optionList: [
                ]
            } ,
           {
                showName: '状态',
                name: 'barcodeStatus',
                type: 'select',
                optionList: [
                    {
                        optionKey: '',
                        optionShow: ''
                    }, {
                        optionKey: '0',
                        optionShow: '可用'
                    }, {
                        optionKey: '1',
                        optionShow: '冻结'
                    }, {
                        optionKey: '2',
                        optionShow: '已出库'
                    }, {
                        optionKey: '3',
                        optionShow: '报废'
                    }
                ]
            },  {
                showName: '冻结原因',
                name: 'lockReason',
                type: 'select',
                optionList: [
                    {
                        optionKey: '',
                        optionShow: ''
                    },
                    // {
                    //     optionKey: '0',
                    //     optionShow: '下线即冻结'
                    // }, {
                    //     optionKey: '1',
                    //     optionShow: '抽检冻结'
                    // },
                     {
                        optionKey: '2',
                        optionShow: '质量冻结'
                    }, {
                        optionKey: '4',
                        optionShow: '手工冻结'
                    }
                ]
            }
            ,{
                name:'amount',
                type: 'number',
                maxName:'amountMax',
                minName:'amountMin'
            }
            //,{
            //     name:'gmtModified',
            //     type: 'date',
            //     maxName:'gmtModifiedMax',
            //     minName:'gmtModifiedMin'
            // }
        ];

    }

    rowHandles = (strHandles, rowObj) => {
        console.log("strHandles===ListResource=======", strHandles);
        if (strHandles === 'edit') {
            this.editHandle(rowObj);
        }
    }

    // editHandle = (rowObj) => {
    //     if (rowObj.length == 0) {
    //         Modal.error({
    //             title: '错误提示',
    //             content: '请选择一条数据',
    //         });
    //     } else if (rowObj.length > 1) {
    //         Modal.error({
    //             title: '错误提示',
    //             content: '只能选择一条数据',
    //         });
    //     } else {
    //         this.refs.updateWorkarea.editHandle(rowObj[0].data);
    //     }
    // }

    editHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else {
            //this.refs.updateDock.editHandle(rowObj[0].data);

            let id = rowObj[0].data.storageSnId;

            let options={
                url: '/material/insidewarehouse/RawStorageSn/findById/' + id,
                type: 'get',//请求方式
                condition: {id: id}

                // url: '/material/insidewarehouse/RawStorageSn/get/' + id,
                // type: 'get',//请求方式
                // condition: {id: id}
            };

            fetchData(this, options, (resData) => {
                if (resData.code == '1'){
                    this.setState({
                        formData:resData.data,
                        submitUrl:'/material/insidewarehouse/RawStorageSn/updateById'
                        // submitUrl: '/material/insidewarehouse/RawStorageSn/out/edit',
                    });
                    this.showModal();
                }else {
                    message.error(resData.msg);
                }
            });
        }

    }

    showModal = () => {
        this.setState({
            visible: true
        });
    }

    handleCancel = () => {
        this.setState({
            visible: false
        });
    }

    componentWillMount() {
        const dicType = 'model_type';
        const options= {
            url: '/base/dic/get/' + dicType,
            type: 'get',
            condition:{}
        };
        //先写死，后面再改
        fetchData(this, options, this.dicInitCallBackHandle);
    }

    //下拉框 初始化
    dicInitCallBackHandle = (resData)=>{
        //alert('resData' + JSON.stringify(resData, null, 4));
        let optionList = [];
        let tmp = {};
        tmp.optionKey = '';
        tmp.optionShow = '';
        optionList.push(tmp);
        resData.data.map( (e,index) => {
            let tmp = {};
            tmp.optionKey = e.dicCode;
            tmp.optionShow = e.dicName;
            optionList.push(tmp);
        });

        //alert('optionList' + JSON.stringify(optionList, null, 4));
        this.tableColumn.data[4].showOption = optionList;

        this.searchComponentData[0].optionList = optionList;
        //this.setState({
        //    tableColumn: this.tableColumn,
        //    searchComponentData: this.searchComponentData
        //})

    }

    //刷新列表
    refreshHandle = () => {
        this.refs.listWorkarea.refreshHandle();
    }

    render() {
        //当左侧隐藏时，列表宽度加上左侧的宽度，
        //1、当前this.props.isHideLeft 是从页面组件containers/Main/main.js传过来
        //2、this.props.isHideLeft 最顶级赋值是在页面 containers/Main/main.js
        let isHideLeft = this.props.isHideLeft;

        return (
            <div>
                <ListBox
                    ref='listWorkarea'
                    title="原材料条码明细"
                    isHideLeft={isHideLeft}
                    tableColumnData={this.tableColumn}
                    searchComponentData={this.searchComponentData}
                    url='/material/insidewarehouse/RawStorageSn/list'
                    rowHandles={this.rowHandles}
                    rowOperation={this.rowOperation}
                    headerOperation={this.headerOperation}
                    isRowSingle="true"
                />

                <Modal
                    style={{top: '20px'}}
                    visible={this.state.visible}
                    title="物料转换"
                    onOk={this.handleOk}
                    onCancel={this.handleCancel}
                    width={'850px'}
                    footer=''
                    maskClosable={false}>
                    <ContractNumberForm
                        onCancel={this.handleCancel}
                        ref="ContractNumberForm"
                        submitUrl={this.state.submitUrl}
                        formData={this.state.formData}
                        init={this.state.visible}
                        refreshHandle={this.refreshHandle}
                    />
                </Modal>

            </div>
        )
    }
}
